package com.arrogames.blondbattle 
{
	import com.arrogames.blondbattle.game.combat.Faction;
	import com.arrogames.blondbattle.game.GameEngine;
	import com.arrogames.blondbattle.model.UnitDefinition;
	import com.arrogames.blondbattle.model.WorldDefinition;
	import com.arrogames.blondbattle.music.MusicManager;
	import com.arrogames.blondbattle.xml.WorldParser;
	import feathers.data.ListCollection;
	import starling.events.Event;
	import starling.events.EventDispatcher;
	import org.as3commons.logging.api.getLogger;
	import org.as3commons.logging.api.ILogger;
	import starling.core.Starling;
	/**
	 * Facade for the game.
	 * @author Robin van den Bogaard
	 */
	public class GameModel extends EventDispatcher {
		
		private static const logger:ILogger = getLogger(GameModel);
		
		private var musicManager:MusicManager;
		private var _availableUnits:ListCollection;
		private var _availableWorlds:ListCollection;
		private var game:GameEngine;
		
		public function GameModel(game:GameEngine, availableWorlds:ListCollection) {
			this.game = game;
			this._availableWorlds = availableWorlds;
			this.musicManager = new MusicManager();
			musicManager.isMuted() ? game.mute() : game.unmute();
		}
		
		public function playGameMusic():void {
			if (!musicManager.isPlayingGameMusic()) {
				musicManager.changeToGameMusic();
			}
		}
		
		public function playMenuMusic():void {
			if (!musicManager.isPlayingMenuMusic()) {
				musicManager.changeToMenuMusic();
			}
		}
		
		public function startGame(world:WorldDefinition):void {
			if (!game.started()) {
				game.setWorld(world);
				game.start();
			}
		}
		
		public function stopGame():void {
			game.stop();
		}
		
		public function mute():void {
			if (!musicManager.isMuted()) {
				musicManager.mute();
				game.mute();
			}
		}
		
		public function unmute():void {
			if (musicManager.isMuted()) {
				musicManager.unmute();
				game.unmute();
			}
		}
		
		public function isMuted():Boolean {
			return musicManager.isMuted();
		}
		
		public function purchaseUnit(unit:UnitDefinition):void {
			game.playerOrderedUnit(unit);
		}
		
		public function get availableUnits():ListCollection {
			return _availableUnits;
		}
		
		public function set availableUnits(value:ListCollection):void {
			_availableUnits = value;
		}
		
		public function get availableWorlds():ListCollection {
			return _availableWorlds;
		}
		
	}

}